package com.dmxh.controller;

import com.dmxh.pojo.TbAreas;
import com.dmxh.pojo.TbCities;
import com.dmxh.pojo.TbProvinces;
import com.dmxh.result.Result;
import com.dmxh.service.AddressService;
import com.dmxh.util.JwtUtil;
import com.dmxh.vo.AddressVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("/queryAllAddress")
    public Result queryAllAddress(@RequestHeader String adminToken) {
        Claims claims = JwtUtil.parseJwt(adminToken);
        String userId = claims.getId();
        List<AddressVo> list = addressService.queryAllAddress(userId);
        return Result.ok().setData(list);
    }

    @GetMapping("/queryAddressById/{id}")
    public Result<AddressVo> queryAddressById(@PathVariable("id")long id) {
        AddressVo addressVo = addressService.queryById(id);
        return Result.ok().setData(addressVo);
    }

    @PostMapping("/save")
    public Result save(@RequestBody AddressVo addressVo) {
        if (addressVo.getId() == null) {
            addressService.addInfo(addressVo);
        } else {
            addressService.updateInfo(addressVo);
        }
        return Result.ok();
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id")long id) {
        addressService.deleteById(id);
        return Result.ok();
    }

    @GetMapping("/queryAllProvince")
    public Result queryAllProvince() {
        List<TbProvinces> tbProvincesList = addressService.queryAllProvince();
        return Result.ok().setData(tbProvincesList);
    }
    @GetMapping("/queryAllCity")
    public Result queryAllCity() {
        List<TbCities> tbProvincesList = addressService.queryAllCity();
        return Result.ok().setData(tbProvincesList);
    }
    @GetMapping("/queryAllArea")
    public Result queryAllArea() {
        List<TbAreas> tbProvincesList = addressService.queryAllArea();
        return Result.ok().setData(tbProvincesList);
    }

}
